#pragma once


template <class T>
struct BSTreeNode
{
	struct BSTreeNode<T>* _left;
	struct BSTreeNode<T>* _right;
	T _key;

	BSTreeNode(const T& key)
		:_key(key)
		, _left(nullptr)
		, _right(nullptr)
	{
	}
};

template <class T>
class BSTree
{
	typedef struct BSTreeNode<T> Node;

private:
	void DestoryTree(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		DestoryTree(root->_left);
		DestoryTree(root->_right);
		delete root;
	}

	Node* CopyTree(Node* t)
	{
		if (t == nullptr)
		{
			return nullptr;
		}

		Node* copyNode = new Node(t->_key);
		copyNode->_left = CopyTree(t->_left);
		copyNode->_right = CopyTree(t->_right);
		return copyNode;
	}

public:

	BSTree() = default;

	BSTree(const BSTree<T>& t)
	{
		_root = CopyTree(t._root);
	}

	BSTree<T>& operator=(BSTree<T> t)
	{
		swap(_root, t._root);
		return *this;
	}

	~BSTree()
	{
		DestoryTree(_root);
		_root = nullptr;
	}

	bool insert(const T& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				break;
			}
		}
		if (cur != nullptr)
		{
			return false;
		}
		cur = new Node(key);
		if (parent->_key > key)
		{
			parent->_left = cur;
			//return true;
		}
		if (parent->_key < key)
		{
			parent->_right = cur;
			//return true;
		}
		return true;

	}

	bool find(const T& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else
			{
				return true;
			}
		}
		return false;
	}

	bool Erase(const T& key)
	{
		Node* cur = _root;
		Node* parent = nullptr;

		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				if (cur->_left == nullptr)
				{
					if (cur == _root)
					{
						//cur = cur->_right;
						_root = cur->_right;
					}
					//if (parent->_left == cur)
					//{
					//	parent->_left = cur->_right;
					//}
					//else if (parent->_right == cur)
					//{
					//	parent->_right = cur->_right;
					//}
					else
					{
						if (parent->_left == cur)
						{
							parent->_left = cur->_right;
						}
						else if (parent->_right == cur)
						{
							parent->_right = cur->_right;
						}
					}
					delete cur;
				}
				else if (cur->_right == nullptr)
				{
					if (cur == _root)
					{
						//cur = cur->_left;
						_root = cur->_left;
					}
					//if (parent->_left == cur)
					//{
					//	parent->_left = cur->_left;
					//}
					//else if (parent->_right == cur)
					//{
					//	parent->_right = cur->_left;
					//}
					else
					{
						if (parent->_left == cur)
						{
							parent->_left = cur->_left;
						}
						else if (parent->_right == cur)
						{
							parent->_right = cur->_left;
						}
					}
					delete cur;
				}
				else
				{
					Node* minparent = cur;
					Node* minright = cur->_right;
					while (minright->_left)
					{
						minparent = minright;
						minright = minright->_left;
					}
					swap(cur->_key, minright->_key);
					if (minparent->_left == minright)
					{
						minparent->_left = minright->_right;
					}
					else if(minparent->_right==minright)
					{
						minparent->_right = minright->_right;
					}
					delete minright;
				}

				return true;
			}
		
		}

		return false;
	}

	void Inorder()
	{
		_Inorder(_root);
		cout << endl;
	}

	bool FindR(const T& key)
	{
		return _FindR(_root, key);
	}

	bool InsertR(const T& key)
	{
		return _InsertR(_root, key);
	}

	bool EraseR(const T& key)
	{
		return _EraseR(_root, key);
	}

private:
	bool _EraseR(Node*& root, const T& key)
	{
		if (root == nullptr)
		{
			return false;
		}
		if (root->_key < key)
		{
			return _EraseR(root->_right, key);
		}
		else if (root->_key > key)
		{
			return _EraseR(root->_left, key);
		}
		else
		{
			Node* del = root;
			if (root->_left == nullptr)
			{
				root = root->_right;
			}
			else if (root->_right == nullptr)
			{
				root = root->_left;
			}
			else
			{
				Node* minRight = root->_right;
				while (minRight->_left)
				{
					minRight = minRight->_left;
				}
				swap(root->_key, minRight->_key);
				return _EraseR(root->_right, key);
			}
			delete del;
			return true;
		}
	}

	bool _InsertR(Node*& root, const T& key)
	{
		if (root == nullptr)
		{
			root = new Node(key);
			return true;
		}
		if (root->_key < key)
		{
			return _InsertR(root->_right, key);
		}
		else if (root->_key > key)
		{
			return _InsertR(root->_left, key);
		}
		else
		{
			return false;
		}
	}

	bool _FindR(Node* root, const T& key)
	{
		if (root == nullptr)
		{
			return false;
		}
		if (root->_key < key)
		{
			return _FindR(root->_right, key);
		}
		else if (root->_key > key)
		{
			return _FindR(root->_left, key);
		}
		else
		{
			return true;
		}
	}

	void _Inorder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_Inorder(root->_left);
		cout << root->_key << " ";
		_Inorder(root->_right);
	}

	Node* _root = nullptr;
};

void TestBSTree1()
{
	BSTree<int> t;
	int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
	for (auto e : a)
	{
		t.insert(e);
	}
	t.Inorder();
	cout << endl;

	t.insert(11);
	t.insert(0);
	t.Inorder();
}

void TestBSTree2()
{
	BSTree<int> t;
	int a[] = { 8,7,9,12,5,19,20,30,7,12 };
	for (auto e : a)
	{
		t.insert(e);
	}
	t.Inorder();
}

void TestBSTree3()
{
	BSTree<int> t;
	int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
	for (auto e : a)
	{
		t.insert(e);
	}
	t.Inorder();
	cout << endl;

	t.Erase(3);
	t.Erase(8);

	t.Inorder();
	cout << endl;

	t.Erase(14);
	t.Erase(7);
	t.Inorder();
	cout << endl;

	for (auto e : a)
	{
		t.Erase(e);
	}
	t.Inorder();
}

void TestBSTree4()
{
	BSTree<int> t;
	int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
	for (auto e : a)
	{
		t.insert(e);
	}
	t.Inorder();

	BSTree<int> copy = t;
	copy.Inorder();

	BSTree<int> m(t);
	m.Inorder();
}

void TestBSTree5()
{
	BSTree<int> t;
	int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
	for (auto e : a)
	{
		t.InsertR(e);
	}
	t.Inorder();

	t.InsertR(9);

	BSTree<int> copy = t;
	copy.Inorder();
}

void TestBSTree6()
{
	BSTree<int> t;
	int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
	for (auto e : a)
	{
		t.InsertR(e);
	}
	t.Inorder();
	t.EraseR(3);
	t.EraseR(8);

	t.Inorder();
	t.EraseR(14);
	t.EraseR(7);
	t.Inorder();

	for (auto e : a)
	{
		t.EraseR(e);
	}
	t.Inorder();
}